import { Component, OnInit } from '@angular/core';
import { HeroService } from '../../../services/hero.service';
import { NzMessageService,NzModalService } from 'ng-zorro-antd';

@Component({
  selector: 'app-acount',
  templateUrl: './acount.component.html',
  styleUrls: ['./acount.component.css']
})
export class AcountComponent implements OnInit {

  loading = true;
  isVisible = false;
  btn_loading=false;
  type = false;
  public localUsername:string;
  public username:string='';
  public name:string='';
  public mobile:string='';
  public password:string;
  public pwdcord:number;
  public roleslist:Array<any>=[];
  public PageIndex:number=1;
  public PageSize:number=10;
  public PageTotal:number;
  constructor(private heroService: HeroService,
              private message: NzMessageService,
              private modalService: NzModalService) { }

  ngOnInit() {
    const username = localStorage.getItem("myUsername");
    this.localUsername=username.substring(1,username.length-1);
    this.getAcountlist();
  }

  //获取列表数据
  getAcountlist(){
    this.btn_loading=true;
    let url=host+"/main/merchantSubAcount/list";
    let model="usernameLike="+this.username+"&nameLike="+this.name+"&mobileLike="+this.mobile+"&page="+this.PageIndex+"&pageSize="+this.PageSize;
    this.heroService.post(url,model).then(result=>{
      this.btn_loading=false;
      if(result.code==0){
        this.roleslist=result.data.pageBean.result;
        this.PageIndex=result.data.pageBean.curPage;  
        this.PageTotal=result.data.pageBean.totalRow;
        this.loading=false;
      }else{
        this.modalService.error({
          nzTitle: '提示',
          nzContent: result.msg
        });
      }
    })
  }

  delete(value){
    console.log(value);
    let url=host+"/main/merchantSubAcount/del";
    let model='id='+value;
    this.heroService.post(url,model).then(result=>{
        console.log(result.code);
        if(result.code==0){
          this.message.success('删除成功！');
          this.getAcountlist();
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  //修改状态
  editStatus(value,getStatus){
    console.log(value);
    let url=host+"/main/merchantSubAcount/stopOrStart";
    if(getStatus==0){
      var status=1;
    }else if(getStatus==1){
      var status=0;
    }
    let model='id='+value+'&status='+status;
    this.heroService.post(url,model).then(result=>{
        console.log(result.code);
        if(result.code==0){
          this.message.success('修改状态成功！');
          this.getAcountlist();
        }else{
          this.modalService.error({
            nzTitle: '提示',
            nzContent: result.msg
          });
        }
    })
  }
  // 重置密码
  resetPwd(){
    this.isVisible = true;
  }
  handleOk(value): void {
    if(this.password){
      this.isVisible = false;
      let url=host+"/main/merchantSubAcount/resetPwd";
      let model='id='+value+'&pwd='+this.password;
      this.heroService.post(url,model).then(result=>{
          console.log(result.code);
          if(result.code==0){
            this.message.success('重置密码成功！');
          }else{
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
      })
    }else{
      this.pwdcord=-1;
    }
  }
  onFocus(){
    this.pwdcord=0;
  }
  handleCancel(): void {
    this.isVisible = false;
  }

  //分页
  PageIndexChange(event){
    this.PageIndex=event;
    this.getAcountlist();
  }
}
